﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public class EquipmentList
	{
		public EquipmentList(UIDocument uiDoc, EquipmentListForm form)
		{
			this.m_uiDoc = uiDoc;
			this.m_doc = this.m_uiDoc.ActiveView.Document;
			this.m_form = form;
		}

		public bool Assignment()
		{
			if (this.m_form == null)
			{
				return false;
			}
			bool isAssignLevel = this.m_form.IsAssignLevel;
			EquipmentListData selectedData = this.m_form.SelectedData;
			if (selectedData == null)
			{
				return false;
			}
			Reference reference = this.m_uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ElectricalFilter(), "请选择要赋值的电气设备：");
			if (reference == null)
			{
				return false;
			}
			Element elementById = this.m_doc.GetElementById(reference.ElementId);
			Transaction transaction = null;
			bool result;
			try
			{
				transaction = new Transaction(this.m_doc, "EquipmentAssignment");
				transaction.Start();
				if (isAssignLevel)
				{
					ElementParameter.SetLevel(elementById, selectedData.Level);
				}
				ElementParameter.SetParameterValueEx(elementById, "编号", selectedData.Number, (Autodesk.Revit.DB.ParameterType)1);
				ElementParameter.SetParameterValueEx(elementById, "名称", selectedData.Name, (Autodesk.Revit.DB.ParameterType)1);
				transaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
				TaskDialog.Show("盈建科提示", ex.Message);
				result = false;
			}
			return result;
		}

		public bool Layout()
		{
			if (this.m_form == null)
			{
				return false;
			}
			int fiWidth = this.m_form.FiWidth;
			int fiHeight = this.m_form.FiHeight;
			int fiDepth = this.m_form.FiDepth;
			EquipmentFamily selectedFamily = this.m_form.SelectedFamily;
			EquipmentListData selectedData = this.m_form.SelectedData;
			if (selectedData == null)
			{
				return false;
			}
			BuiltInCategory category = BuiltInCategory.OST_ElectricalEquipment;
			string familyFile = this.GetFamilyFile(selectedFamily, ref category);
			FamilySymbol familySymbol = HYFamilyUtils.FamilySymbol(this.m_doc, familyFile, category);
			if (familySymbol == null)
			{
				TaskDialog.Show("盈建科提示", "加载族失败。有可能族文件不存在导致。");
				return false;
			}
			Transaction transaction = null;
			bool result;
			try
			{
				List<ElementId> elements = this.GetElements(category);
				this.PromptForFamilyInstancePlacement(familySymbol);
				List<ElementId> elements2 = this.GetElements(category);
				List<ElementId> list = new List<ElementId>();
				foreach (ElementId item in elements2)
				{
					if (!elements.Contains(item))
					{
						list.Add(item);
					}
				}
				transaction = new Transaction(this.m_doc, "EquipmentLayout");
				transaction.Start();
				foreach (ElementId elementId in list)
				{
					Element element = this.m_doc.GetElement(elementId);
					if (element != null)
					{
						ElementParameter.SetLevel(element, selectedData.Level);
						ElementParameter.SetParameterValue(element, "宽度", fiWidth);
						ElementParameter.SetParameterValue(element, "高度", fiHeight);
						ElementParameter.SetParameterValue(element, "进深", fiDepth);
						ElementParameter.SetParameterValueEx(element, "编号", selectedData.Number, (Autodesk.Revit.DB.ParameterType)1);
						ElementParameter.SetParameterValueEx(element, "名称", selectedData.Name, (Autodesk.Revit.DB.ParameterType)1);
					}
				}
				transaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
				TaskDialog.Show("盈建科提示", ex.Message);
				result = false;
			}
			return result;
		}

		private bool PromptForFamilyInstancePlacement(FamilySymbol jigType)
		{
			bool result;
			try
			{
				this.m_uiDoc.PromptForFamilyInstancePlacement(jigType);
				result = true;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private string GetFamilyFile(EquipmentFamily enumFamily, ref BuiltInCategory category)
		{
			string result = string.Empty;
			category = BuiltInCategory.OST_ElectricalEquipment;
			if (enumFamily != EquipmentFamily.Family_SwitchBox)
			{
				if (enumFamily != EquipmentFamily.Family_Mechanical)
				{
					result = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.m_doc), "CableLay", "电气设备.rfa");
					category = BuiltInCategory.OST_ElectricalFixtures;
				}
				else
				{
					result = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.m_doc), "CableLay", "机电设备.rfa");
					category = BuiltInCategory.OST_ElectricalFixtures;
				}
			}
			else
			{
				result = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.m_doc), "CableLay", "箱柜.rfa");
				category = BuiltInCategory.OST_ElectricalEquipment;
			}
			return result;
		}

		private List<ElementId> GetElements(BuiltInCategory category)
		{
			return new FilteredElementCollector(this.m_doc).OfCategory(category).OfClass(typeof(FamilyInstance)).ToElementIds().ToList<ElementId>();
		}

		private const string Family_Param_Width = "宽度";

		private const string Family_Param_Height = "高度";

		private const string Family_Param_Depth = "进深";

		private UIDocument m_uiDoc;

		private Document m_doc;

		private EquipmentListForm m_form;
	}
}
